package me.zxk.qwert_poetry.dto.response;

import lombok.Data;
import me.zxk.qwert_poetry.dao.Author;
import me.zxk.qwert_poetry.dao.Poem;
import me.zxk.qwert_poetry.dao.Tag;
import me.zxk.qwert_poetry.dao.User;
import me.zxk.qwert_poetry.service.PoemService;
import me.zxk.qwert_poetry.util.UserContextHolder;

import java.util.List;

@Data
public class PoemSimpleResponseDTO {
    private Integer id;
    private String title;
    private Author author;
    private List<Tag> tagIds;
    private Integer likeCount;
    private Boolean liked;
    private Boolean collected;
    private Integer collectionCount;

    public static PoemSimpleResponseDTO fromEntity(Poem poem, PoemService poemService) {
        PoemSimpleResponseDTO dto = new PoemSimpleResponseDTO();
        dto.setId(poem.getId());
        dto.setTitle(poem.getTitle());
        dto.setAuthor(poem.getAuthor());
        dto.setTagIds(poem.getTags());

        User user = UserContextHolder.getUser();
        if (user != null) {
            dto.setLiked(poemService.isLiked(poem.getId(), user.getId()));
            dto.setCollected(poemService.isCollected(poem.getId(), user.getId()));
        }
        dto.setLikeCount(poemService.getLikeCountById(poem.getId()));
        dto.setCollectionCount(poemService.getCollectCountById(poem.getId()));
        return dto;
    }
}
